WS-Discovery is a multicast protocol used to discover
services within a network. It provides a standardized means of
discovering services at runtime by allowing services to locate other
services using UDP multicast messages or via a discovery proxy.
To find a target
service, a service consumer sends a probe message to a multicast group.
Target services that match the probe then send responses directly to the
service consumer informing it of their existence. A service consumer
can also look for endpoint address changes if a service’s network
location changed.
Version 4.0 of the
.NET framework introduced the WCF Discovery extension, a mechanism
based on the WS-Discovery industry standard. It enables service
redundancy, dynamic load balancing, and even fault tolerance (where if
one service fails to respond, another service that satisfies the
criteria can be dynamically located).
Being
able to discover services dynamically simplifies the maintenance of
service dependencies and increases the overall robustness of a service
inventory because an alternative to a failed or unavailable service can
quickly be located without explicit reconfiguration steps. Dynamic
discovery and announcements can also be necessary in highly dynamic
scenarios, such as in a dynamic cloud environment where new service
instances go online or offline, depending on current usage patterns.
Discovery Modes
WCF supports two discovery modes: ad hoc and managed:
Ad hoc discovery
allows services to locate other services on their local subnet and
announce their availability when they go online or offline. Each service
on the network can receive and respond to multicast discovery queries
for targeted discovery needs.
Managed discovery
introduces a dedicated discovery proxy into the environment. This proxy
manages service availability information and responds to discovery
queries to reduce the overall network traffic related to discovery
announcements and queries and further allows for the discovery of
services available outside the local subnet.
All classes related to the WS-Discovery protocol are encapsulated in the System.ServiceModel.Discovery library.
A service can respond to probe
messages by adding a well-known endpoint and service behavior to manage
the endpoint. As it is standard practice in WCF, both can be set
programmatically or via configuration files.
The UDP endpoint is a standard WCF endpoint. The next example shows the endpoint element adding a discovery endpoint. Specifically, the kind attribute (introduced with .NET 4.0) is used to identify the standard endpoints:
Example 1.
<services> <service name="Example.Services.CustomerService" behaviorConfiguration="DiscoveryBehavior">
... service application endpoint ...
<endpoint name="udpDiscoveryEpt" kind="udpDiscoveryEndpoint" /> </service> </services>
|
Note
that standard endpoints are configured machine-wide in the
Machine.Config file. You can override the configuration in the
Web.Config file, if necessary.
WCF 4.0 includes a service
behavior to manage probe requests received on the discovery endpoint.
This behavior is added to the service’s configuration, as shown here:
Example 2.
<behaviors> <serviceBehaviors> <behavior name="DiscoveryBehavior"> <serviceDiscovery /> </behavior> </serviceBehaviors> </behaviors>
|
A probe request includes a
number of “find” criteria, a service contract, or a service’s URL scope.
If a service matches all the find criteria, it responds with a FindMatch response containing its location, the matching criteria, and available metadata.
The probing service can
retrieve the metadata from the response and evaluate it to determine how
to handle the discovered service.
Shown here is an endpoint configuration with custom discovery metadata:
Example 3.
<endpoint
address=""
binding="basicHttpBinding"
contract="ICustomerService"
behaviorConfiguration="CustomMetadataBehavior" />
<endpointBehaviors>
<behavior name="CustomMetadataBehavior">
<endpointDiscovery enabled="true">
<extensions>
<MyCustomMetadata>Highly Scalable</MyCustomMetadata>
</extensions>
</endpointDiscovery>
</behavior>
</endpointBehaviors>